﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_news_add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RadioButtonList _RBL_readstate = (RadioButtonList)FormView1.FindControl("RadioButtonList2");
            //if (useradminid > 0)
            //{
            _RBL_readstate.SelectedValue = "3";
            //}
            //else
            //{
            //    _RBL_readstate.SelectedValue = "2";
            //    _RBL_readstate.Items.FindByValue("3").Enabled = false;
            //}


            TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox3");
            _TextBox_text.Text = System.DateTime.Now.ToString();
            
            _TextBox_text = (TextBox)FormView1.FindControl("TextBox14");
            _TextBox_text.Text = "本站原创";

            _TextBox_text = (TextBox)FormView1.FindControl("TextBox13");
            _TextBox_text.Text = "Admin";


            help.Visible = false;

        }
    }



    protected void Button2_Click(object sender, EventArgs e)
    {
        help.Visible = true;
    }
    protected void Button5_Click1(object sender, EventArgs e)
    {
        help.Visible = false;
    }



    protected void Button3_Click(object sender, EventArgs e)
    {
        FileUpload fileupload_pic = (FileUpload)FormView1.FindControl("MyFileUpload");
        if (fileupload_pic.PostedFile.FileName.ToString() != "")
        {
            Label label_Msg = (Label)FormView1.FindControl("Label_Msg");  //提示信息标签

            string photoname = fileupload_pic.PostedFile.FileName;
            int i = photoname.LastIndexOf(".");
            string file_next_name = photoname.Substring(i);
            if (file_next_name.ToLower() != ".gif" && file_next_name.ToLower() != ".jpg" && file_next_name.ToLower() != ".jepg" && file_next_name.ToLower() != ".bmp" && file_next_name.ToLower() != ".png")
            {
                Response.Write("<script language='Javascript'>alert('上传文件出错,并不是图片文件！');</script>");
            }
            else
            {
                string filename = DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Second.ToString() + file_next_name; // 文件名称
                string filename_s = "s_" + filename; // 缩略图文件名称
                string webFilePath = Server.MapPath("../upload/news/" + filename); // 服务器端文件路径
                string webFilePath_s = Server.MapPath("../upload/news/" + filename_s);　　// 服务器端 中 缩略图路径
                string webFilePath_s_s = Server.MapPath("../upload/news/s_" + filename_s);　　// 服务器端 小 缩略图路径


                if (!File.Exists(webFilePath))
                {
                    try
                    {
                        fileupload_pic.PostedFile.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
                        //Discuz.Web.global.global.MakeThumbnail(webFilePath, webFilePath_s, 280, 220, "Cut"); // 生成缩略图方法
                        //Discuz.Web.global.global.MakeThumbnail(webFilePath, webFilePath_s_s, 130, 95, "Cut"); // 生成缩略图方法

                        TextBox _TextBox_text1 = (TextBox)FormView1.FindControl("TextBox8");
                        _TextBox_text1.Text = "../upload/news/" + filename_s.Trim();

                        Image ImageUpload = (Image)FormView1.FindControl("Image1");
                        ImageUpload.Visible = true;
                        ImageUpload.ImageUrl = "../upload/news/" + filename_s;


                        label_Msg.Text = "提示：图片上传成功，并生成缩略图，大小：" + fileupload_pic.PostedFile.ContentLength / 1024 + "K";//，文件类型为：" + fileupload_pic.PostedFile.ContentType + "
                    }
                    catch (Exception ex)
                    {
                        label_Msg.Text = "提示：文件上传失败，失败原因：" + ex.Message;
                    }
                }
                else
                {
                    label_Msg.Text = "提示：图片已经存在，请重新上传";
                }
            }
        }
        else
        {
            Response.Write("<script language='Javascript'>alert('上传图片文件不能为空！');</script>");
        }
    }

    /*
        public string Format(object input)
        {
            string data = input.ToString();
            data = data.Replace("'", "&qapos;");
            return data;
        }*/

    protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        //e.Values["userid"] = userid;
        //e.Values["subclassid"] = Convert.ToInt32(Request.QueryString["subclassid"].ToString());
        //e.Values["mainclassid"] = Convert.ToInt32(Request.QueryString["classid"].ToString());

        //TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox13");
        //if (_TextBox_text.Text.Trim() == "")
        //{
        //    e.Values["username"] = username.Trim();
        //}

        //DropDownList _DropDownList_Category = (DropDownList)FormView1.FindControl("DropDownList3");
        //if (_DropDownList_Category != null)
        //    e.Values["subclassid"] = _DropDownList_Category.SelectedValue;
        //else
        //{
        //    //Response.Write("<script language='Javascript'>alert('分类无效，请选择有效的分类！');</script>");
        //    e.Cancel = true;
        //}

        //TextBox _TextBoxTitle = (TextBox)FormView1.FindControl("TextBox1");
        //if (_TextBoxTitle.Text == "")
        //{
        //    Response.Write("<script language='Javascript'>alert('添加新闻出错,可能你的新闻标题未填写！');</script>");
        //    e.Cancel = true;
        //}
        //Response.Write("<script language='Javascript'>alert('添加新闻出错,可能你的新闻标题未填写！');</script>");

        //Label1.Text = e.Values["title"].ToString();
        //string title11 = e.Values["title"].ToString();
        //if (e.Values["title"].ToString() == null)
        //{
        //    Response.Write("<script language='Javascript'>alert('添加新闻出错,可能你的新闻标题未填写！');</script>");
        //    return;
        //    //Label1.Text = "添加新闻出错,可能你的新闻标题未填写";
        //}
        //else if (e.Values["title"] TextBox_Title.Text.Length > 80)
        //{
        //    Response.Write("<script language='Javascript'>alert('文章标题长度过长,请缩短后再填写！');</script>");
        //}


        //if (e.Values[""] FormView1.DataItem.FormView1.DataItem.ToString() == "")
        // {
        //     Label1.Text = "添加新闻出错,可能你的新闻标题未填写";
        // }
        // else if (80 > 50)
        // {
        //     Label1.Text = "博客文章标题长度过长,请缩短后再填写";
        // }
        //DataRowView rowView = (DataRowView)FormView1.DataItem;
        //Label1.Text = FormView1.DataItemCount.ToString();// rowView.Row.ToString();/;/ ["title"].ToString(); //"Good Morning!";

    }
    protected void InsertButton_Click(object sender, EventArgs e)
    {
        //Label1.Text=e.
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox13");
        _TextBox_text.Text = "未知";
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox13");
        _TextBox_text.Text = "佚名";
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox13");
        _TextBox_text.Text = "Admin";
    }
    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        //TextBox _TextBoxTitle = (TextBox)FormView1.FindControl("TextBox1");
        Response.Write("<script language='Javascript'>alert('成功的添加了名为  \"" + e.Values["title"].ToString() + "\"  的新闻！');</script>");
        //string url = "news_manage.aspx?classid=" + Request.QueryString["classid"].ToString().Trim() + "&subclassid=" + Request.QueryString["subclassid"].ToString().Trim();
        string url = "news_manage.aspx";
        Response.Redirect(url);//?subclassid="+e.Values["subclassid"] +"\"");


    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox14");
        _TextBox_text.Text = "本站原创";
    }
}
